package com.data.modules.smzh.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.common.constant.enums.ProductEnum;
import com.data.modules.smzh.entity.*;
import com.data.modules.smzh.mapper.FacedeviceMapper;
import com.data.modules.smzh.mapper.SmUserMapper;
import com.data.modules.smzh.service.*;
import com.data.modules.smzh.vo.ReqUserVo;
import com.data.modules.smzh.vo.SmUserInfoVO;
import com.data.modules.smzh.vo.SmUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;


/**
 * @Description: user
 * @Author: data-boot
 * @Date:   2022-11-27
 * @Version: V1.0
 */
@Service
public class SmUserServiceImpl extends ServiceImpl<SmUserMapper, SmUser> implements ISmUserService {

    @Autowired
    private IUserFaceService userFaceService;
    @Autowired
    private IUserCardService cardService;

    @Autowired
    private IBuildinguserService buildUserService;
    @Autowired
    private SmUserMapper smUserMapper;

    @Autowired
    private FacedeviceMapper facedeviceMapper;

    @Autowired
    private IBuildingService buildingService;

    @Autowired
    private IDeviceuserService deviceuserService;
    @Override
    public  IPage<SmUserVo> pageList(Page<SmUserVo> page, SmUserVo user){


        return  smUserMapper.queryPageList(page,user);
    }

    /**
     * 新增用户基本信息
     * @param user
     */
    @Override
    @Transactional
    public void addUserInfo(SmUserVo user) {
        SmUser smUser = new SmUser();

        BeanUtil.copyProperties(smUser,user);

        smUserMapper.insert(smUser);
        if(ObjectUtil.isNotEmpty(smUser.getId())){
            //新增人脸信息
            if(ObjectUtil.isNotEmpty(user.getFaseImage())){

                UserFace userFace = new UserFace();
                userFace.setUserId(smUser.getId());
                userFace.setImagePath(user.getFaseImage());
                userFaceService.save(userFace);

            }

            if(ObjectUtil.isNotEmpty(user.getCardNo())){

                UserCard userCard = new UserCard();
                userCard.setUserId(smUser.getId());
                userCard.setCardType(1);
                userCard.setCardNumber(user.getCardNo());
                cardService.save(userCard);
            }

            if(ObjectUtil.isNotEmpty(user.getUniqueNo())){

                Buildinguser buildinguser = new Buildinguser();
                buildinguser.setUserId(smUser.getId());
                buildinguser.setUniqueNo(user.getUniqueNo());
                buildUserService.saveOrUpdate(buildinguser);
            }

        }

    }

    @Override
    public IPage<SmUserVo> queryBindUserPageList(Page<SmUserVo> page, SmUserVo user) {

        return  smUserMapper.getBindUserPageList(page,user);
    }

    /**
     * 通过设备编号查询 绑定人员信息
     * @param page
     * @param user
     * @return
     */
    @Override
    public IPage<SmUserVo> queryBindUserBySerNoPageList(Page<SmUserVo> page, SmUserVo user) {

        return  smUserMapper.getBindUserBySerNoPageList(page,user);
    }

    @Override
    public List queryUserInfoByIds(List<String> arrary) {


        return smUserMapper.searchUserInfo(arrary);
    }

    //更新用户
    @Override
    @Transactional
    public Integer updateUserInfo(ReqUserVo user, String appId) {

        return 0;
    }

    /**
     * 数据看板统计
     * @param name
     * @return
     */
    @Override
    public Map queryData(String name) {

        Map<String,Object> map = new HashMap();
        List<Building> list = buildingService.queryData(name);
        Integer countUser = smUserMapper.queryUser(name);

        //分组统计
        Map<String, List<Building>> buildNo = list.stream()
                .collect(Collectors.groupingBy(item -> item.getProjectNo() + '-' + item.getBuildNo()));

        Map<String, List<Building>> unitNo = list.stream()
                .collect(Collectors.groupingBy(item ->item.getProjectNo() + item.getBuildNo() + '-' + item.getUnitNo()));

        map.put("buildNum",buildNo.size());
        map.put("unitNum",unitNo.size());
        map.put("roomNum",list.size());
        map.put("userNum",countUser);

        return map;
    }

    @Override
    public List<Map> queryDeviceData(String name) {

        return smUserMapper.searchDeviceTotal(name);
    }

    @Override
    public List<Map> queryAddUserTotal(String name) {
        return smUserMapper.searchAddUserInfo(name);
    }

    @Override
    public Map queryBindUserAndDevice(SmUserInfoVO user) {

        Map<String,Object> map = new HashMap<>();

        List<Devicebuild> list = smUserMapper.queryBindUserAndDevice(user);

        list = list.stream().filter(x->(x.getModel().equals(ProductEnum.SMW_721AC.getCode())
                || x.getModel().equals(ProductEnum.SMW_721A10.getCode())
                || x.getModel().equals(ProductEnum.SMW_721A10W.getCode()))).collect(Collectors.toList());;
        if(ObjectUtil.isNotEmpty(list)){

            List<String>serialNos = list.stream().map(Devicebuild::getSerialNo).collect(Collectors.toList());

            map.put("serialNos",serialNos);

            map.put("userId",list.get(0).getId());
        }

        return map;
    }

    @Override
    public List<Map> authPageList(SmUserInfoVO user) {

        //获取 appId
        String appId = facedeviceMapper.queryDevelAppId(String.valueOf(user.getId()));
        user.setAppId(Integer.valueOf(appId));

        return smUserMapper.searchAuthInfo(user);
    }

    @Override
    public SmUser queryUserInfoByid(String id) {

        return smUserMapper.searchUserById(id);
    }
}
